#Tables 2 and A20

#This Script produces both Table 2 in the main text and the full version of
#the same Table (A13) in the Appendix

rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()

library(stargazer)
library(plm)
library(lmtest)


here::i_am("Scripts/Tab2A13Script.R")

library(here)


##First let's get the data####
df<-readRDS(here("Data", "analysis_data2.rds"))
            
  #preliminary code: data to delete
 d1<-setdiff(colnames(df)[grep("3",colnames(df))],"statechamberyear3")
 d2<-setdiff(colnames(df)[grep("all2",colnames(df))],"statechamberyear3")
 d3<-setdiff(colnames(df)[grep("15",colnames(df))],"statechamberyear3")
 d4<-setdiff(colnames(df)[grep("30",colnames(df))],"statechamberyear3")
 d5<-setdiff(colnames(df)[grep("50",colnames(df))],"statechamberyear3")
 d6<-setdiff(colnames(df)[grep("75",colnames(df))],"statechamberyear3")
 d7<-setdiff(colnames(df)[grep("_ch_",colnames(df))],"statechamberyear3")
 d8<-setdiff(colnames(df)[grep("[a-z]2",colnames(df))],
             c("tenure2","year2","filerid2","v225","v256","median_income_acs5_infl_adj2010","cleaned_tenure2","stateyear2","yearsess2","chair_no_other2","statechamberyear2","exc2","exc22"))
 d9<-setdiff(colnames(df)[grep("totco",colnames(df))],"statechamberyear3")
 d90<-setdiff(colnames(df)[grep("logiamount",colnames(df))],"statechamberyear3")
 d91<-setdiff(colnames(df)[grep("_ine_",colnames(df))],"statechamberyear3")
 d92<-setdiff(colnames(df)[grep("_ni_",colnames(df))],"statechamberyear3")
 d93<-setdiff(colnames(df)[grep("_all_",colnames(df))],"statechamberyear3")
 d94<-setdiff(colnames(df)[grep("_s",colnames(df))],
              c("shormccarty_np_score","leader_speakerpres","dno_string"))
 d95<-setdiff(colnames(df)[grep("_no_ret",colnames(df))],"statechamberyear3")
 d96<-setdiff(colnames(df)[grep("_all",colnames(df))],"statechamberyear3")
 d97<-setdiff(colnames(df)[grep("pct_amount_ne",colnames(df))],"statechamberyear3")
 d98<-c("totam_ine","totam_ins","dup_candidyear","added1","added0","abs_tw_mrp_mean","tw_mrp_mean","leaderxtime","shormccarty_np_score","incumbent_chamber",
        "geoid","chairxtime","speakerpowerxspeaker","amountindustry_1","log_amountindustry_1","pct_etotals_i","median_income_acs5_infl_adj2010","popnum","variable",
        "pct_amountindustry_1","moe","label","concept","survey","etotam_i_ret","pfrni","pfrni_nl","pfri_nl","pfr_nl","netotam_i_ret","shormccarty_u_id")
 d99<-setdiff(colnames(df)[grep("pct_amount_e",colnames(df))],"statechamberyear3")
 d89<-setdiff(colnames(df)[grep("etotam_[a-z]",colnames(df))],
              colnames(df)[grep("etotam_i",colnames(df))])
 
 df<-df[,which(colnames(df)%in%unique(c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d90,d91,d92,d93,d94,d95,d96,d97,d98,d99,d89))==F)]
 
 
 #Table 2: Effects of Legislative Positions on Event Donations from Individuals (events_access.tex)
 
 #(1)
 m1<-plm(logfriamount~as.factor(statechamberyear3)+leader+chair_no_other2+chairs_other+incmaj+incumbent_legislature,data=df[which(is.na(df$exc0)==T&df$limit1!=1),],index=c("cleaned_candid"),model="within")
 #process for getting standard errors as in STATA
 m1b<-within_intercept(m1,return.model = T)
 vc1<-vcovHC(m1b, method = "arellano",cluster="group",type="sss")
 m1c<-coeftest(m1b, vcov.=vc1)
 
 #(2)
 m2<-plm(logfriamount~as.factor(statechamberyear3)+leader_majleader+leader_minleader+leader_speakerpres+chair_no_other2+chairs_other+incmaj+incumbent_legislature,data=df[which(is.na(df$exc0)==T&df$limit1!=1),],index=c("cleaned_candid"),model="within")
 #process for getting standard errors as in STATA
 m2b<-within_intercept(m2,return.model = T)
 vc2<-vcovHC(m2b, method = "arellano",cluster="group",type="sss")
 m2c<-coeftest(m2b, vcov.=vc2)
 
 #output results for paper (Table 2)
 stargazer(m1c,m2c,title="Table 2: Effects of Legislative Positions on Event Donations From Individuals",
           column.labels=c("Ln Event Donations","Ln Event Donations"),
           out=here("Results","Tab2.tex"),
           no.space = T,column.separate = c(1,1),dep.var.labels.include = F,
           keep=c("leader","leader_majleader","leader_minleader","leader_speakerpres","chair_no_other2","incmaj"),
           order=c("leader","leader_majleader","leader_minleader","leader_speakerpres","chair_no_other2","incmaj"),
           covariate.labels = c("Leader","Majority Leader","Minority Leader","Speaker/President","Main Chair","Majority Party"),
           star.char = c("*", "**", "***"),digits = 2,
           add.lines = list(c("Legislator FE",paste("\\","checkmark",sep=""),paste("\\","checkmark",sep="")),
                            c("Chamber-Session-Cohort FE",paste("\\","checkmark",sep=""),paste("\\","checkmark",sep="")),
                            c("Legislators","1,509","1,509"),
                            c("Observations","4,695","4,695")),
           omit.stat = c("rsq","f","adj.rsq"))
 
 
 #output results for appendix (Table A13)
 stargazer(m1c,m2c,title="Table A13: Effects of Legislative Positions on Event Donations From Individuals",
           column.labels=c("Ln Event Donations","Ln Event Donations"),
           out=here("Results","TabA13.tex"),
           no.space = T,column.separate = c(1,1),dep.var.labels.include = F,
           keep=c("leader","leader_majleader","leader_minleader","leader_speakerpres","chair_no_other2","incmaj","chairs_other","incumbent_legislature"),
           order=c("leader","leader_majleader","leader_minleader","leader_speakerpres","chair_no_other2","incmaj","chairs_other","incumbent_legislature"),
           covariate.labels = c("Leader","Majority Leader","Minority Leader","Speaker/President","Main Chair","Majority Party","Other Chair","Legislative Seniority"),
           star.char = c("*", "**", "***"),digits = 2,
           add.lines = list(c("Legislator FE",paste("\\","checkmark",sep=""),paste("\\","checkmark",sep="")),
                            c("Chamber-Session-Cohort FE",paste("\\","checkmark",sep=""),paste("\\","checkmark",sep="")),
                            c("Legislators","1,509","1,509"),
                            c("Observations","4,695","4,695")),
           omit.stat = c("rsq","f","adj.rsq"))
 
